package com.qiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiyu.common.ServiceException;
import com.qiyu.dao.HomeBannerMapper;
import com.qiyu.entity.HomeBanner;
import com.qiyu.model.request.backend.HomeBannerReq;
import com.qiyu.service.HomeBannerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 首页banner图
 *
 * @author zhangweiwei
 * @date 2020-07-30
 */
@Slf4j
@Service
public class HomeBannerServiceImpl extends ServiceImpl<HomeBannerMapper, HomeBanner> implements HomeBannerService {
    @Override
    public HomeBanner saves(HomeBannerReq req) {
        if(StringUtils.isEmpty(req.getHandler())){
            throw new ServiceException("请传入handler");
        }
        HomeBanner homeBanner = new HomeBanner();

        switch (req.getHandler()){
            case "query":
                homeBanner = this.query(req);
                break;
            case "update":
                homeBanner = this.update(req);
                break;
            case "add":
                homeBanner = this.add(req);
                break;
            case "del":
                homeBanner  = this.del(req);
                break;
            default:
                throw new ServiceException("handler 传值不正确");
        }

        return homeBanner;
    }

    @Override
    public IPage<HomeBanner> queryAll(Integer page,Integer size,String bannerName) {
       if(StringUtils.isEmpty(page)){
           throw new ServiceException("请传入page");
       }
       if(StringUtils.isEmpty(size)){
           throw new ServiceException("请传入size");
       }


        QueryWrapper<HomeBanner> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .orderByAsc(HomeBanner::getSort)
                .orderByDesc(HomeBanner::getUpdateTime);

        if(!StringUtils.isEmpty(bannerName)){
            wrapper.lambda(). like(HomeBanner::getBannerName,bannerName);
        }
        IPage<HomeBanner> pages = new Page<>(page,size);
        IPage<HomeBanner> homeBanners = this.page(pages, wrapper);
        return homeBanners;
    }

    @Override
    public void online(Boolean online, Long[] ids) {
        if(StringUtils.isEmpty(online)){
            throw new ServiceException("请传入online的值 ：false 或者 true");
        }
        if(StringUtils.isEmpty(ids)){
            throw new ServiceException("请传入上下线的id");
        }
        for(Long id:ids){
            HomeBanner HomeBanner = this.getById(id);
            HomeBanner.setOnline(online);
            saveOrUpdate(HomeBanner);
        }
    }

    @Override
    public void top(String handler, Long id) {
        if(StringUtils.isEmpty(handler)){
            throw new ServiceException("请传入handler");
        }
        if(StringUtils.isEmpty(id)){
            throw new ServiceException("请传入Id");
        }
        QueryWrapper<HomeBanner> wrapper = new QueryWrapper<>();
        HomeBanner homeBanner = this.getById(id);
        Integer curSort = homeBanner.getSort();
        switch (handler){
            case "top": //置顶

                homeBanner.setSort(1);

                break;
            case "foot": //置底
                wrapper.lambda().orderByDesc(HomeBanner::getSort).last("limit 1");
                Integer minSort = this.getOne(wrapper).getSort();
                homeBanner.setSort(++minSort);
                break;
            case "up": //上一个
                if(curSort>1){
                    homeBanner.setSort(--curSort);
                }
                break;
            case "down": //下一个
                homeBanner.setSort(++curSort);
                break;
            default:
                throw new ServiceException("handler传值不正确");
        }
        saveOrUpdate(homeBanner);
    }

    /**
     * 删除
     * @param req
     * @return
     */
    private HomeBanner del(HomeBannerReq req) {
        HomeBanner homeBanner = this.query(req);
        this.checkHomeBanner(homeBanner);
        homeBanner.setDeleted(true);
       this.removeById(req.getId());
        return this.query(req);

    }
    private  void checkHomeBanner(HomeBanner homeBanner){
        if(homeBanner == null){
            throw new ServiceException("未查询到相关banner");
        }
    }

    /**
     * 添加
     * @param req
     * @return
     */
    private HomeBanner add(HomeBannerReq req) {
        QueryWrapper<HomeBanner> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(HomeBanner::getSort).last("limit 1");
        //获取最大sort
        HomeBanner banner = this.getOne(wrapper);
        Integer maxSort =1 ;
        if(banner != null){
            maxSort = banner.getSort();
            ++maxSort;
        }

        HomeBanner homeBanner = new HomeBanner();
        BeanUtils.copyProperties(req,homeBanner);
        //设置排序
        homeBanner.setSort(maxSort);
        homeBanner.setId(null);
        saveOrUpdate(homeBanner);

        //返回最新添加的数据
        wrapper.lambda().orderByDesc(HomeBanner::getCreateTime);
        homeBanner  = this.getOne(wrapper);
        return homeBanner;
    }

    /**
     * 查询
     * @param req
     * @return
     */
    private HomeBanner query(HomeBannerReq req){
        this.checkId(req);
        HomeBanner homeBanner =  this.getById(req.getId());
        return  homeBanner;
    }

    /**
     * 修改
     * @param req
     * @return
     */
    private HomeBanner update(HomeBannerReq req){
        HomeBanner homeBanner = this.query(req);
        this.checkHomeBanner(homeBanner);

        BeanUtils.copyProperties(req,homeBanner);
        saveOrUpdate(homeBanner);
        return this.query(req);

    }

    /**
     * 校验Id
     * @param req
     */
    private void checkId(HomeBannerReq req){
        if(StringUtils.isEmpty(req.getId()) || req.getId() == 0){
            throw new ServiceException("请传入id");
        }
    }
}
